mini.parse();

function GoodsBrandHandler() {
};

GoodsBrandHandler.instance = new GoodsBrandHandler();

GoodsBrandHandler.isQueryBtnExpended = false;

//获得全局的商品品牌数据表格对象
GoodsBrandHandler.goodsBrandGrid = mini.get("goodsBrandGrid");

//加载表格数据
GoodsBrandHandler.goodsBrandGrid.load();

//品牌状态数据
GoodsBrandHandler.brandStateData = [
	{text: '全部', id: -1},
	{text: '无效', id: 0},
	{text: '有效', id: 1}
];

mini.get("query_bgbState").setData(GoodsBrandHandler.brandStateData);

/**
 * 根据用户权限展示功能按钮
 */
GoodsBrandHandler.loadToolbar = function() {
	var array = new Array("goods_brand_add", "goods_brand_edit", "goods_brand_del","goods_brand_query","goods_brand_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
GoodsBrandHandler.loadToolbar();

/**
 * 弹出添加商品品牌窗口
 */
GoodsBrandHandler.addGoodsBrand = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(371, 415, "添加商品品牌");
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("goodsBrandForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.GoodsBrandFormHandler.passData(data);
	},function(action){
		GoodsBrandHandler.goodsBrandGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑商品品牌窗口
 */
GoodsBrandHandler.editGoodsBrand = function() {
	var rowData = GoodsBrandHandler.goodsBrandGrid.getSelected();
	var rowsData = GoodsBrandHandler.goodsBrandGrid.getSelecteds();
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(371, 415, "编辑商品品牌[<font color='blue'>"	+ rowData.bgbName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("goodsBrandForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bgbId: rowData.bgbId};
            iframe.contentWindow.GoodsBrandFormHandler.passData(data);
		},function(action){
			GoodsBrandHandler.goodsBrandGrid.reload();
		});
		
		miniWin.show();
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除商品品牌资源
 */
GoodsBrandHandler.delGoodsBrand = function() {
	var rowsData = GoodsBrandHandler.goodsBrandGrid.getSelecteds();
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bgbId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bgbId;
					
					// 判断选择的品牌记录中是否已被使用
					if(GoodsBrandHandler.getBgiByBgb(ids)){
						MiniuiTools.miniWarning("系统提示", "选择的记录存在商品信息关联，请检查后重试");
						return;	
					}
					
					GoodsBrandHandler.goodsBrandGrid.loading();
                    $.ajax({
                        url: __root + "/delGoodsBrandByIds.do",
                        data: {
				        	bgbIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示", "操作成功！", function(){
				            	GoodsBrandHandler.goodsBrandGrid.reload();
				            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
            				MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(e){
                        	GoodsBrandHandler.goodsBrandGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 检验选择的记录是否已被商品使用
 * @param {} bsiIds
 * @return {}
 */
GoodsBrandHandler.getBgiByBgb = function(bgbIds){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBgiByBgb.do?bgbIds=" + bgbIds,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 更新商品品牌状态
 */
GoodsBrandHandler.updateGoodsBrandState = function() {
	if (!MiniuiTools.hasPurview("goods_brand_edit")) {
		return;
	}
	var rowData = GoodsBrandHandler.goodsBrandGrid.getSelected();
	var bgbId = rowData.bgbId;	
	GoodsBrandHandler.goodsBrandGrid.loading();
	
    $.ajax({
        url: __root + "/updateGoodsBrandStateById.do",
        data: {
        	bgbId: bgbId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            GoodsBrandHandler.goodsBrandGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GoodsBrandHandler.goodsBrandGrid.unmask();
        }
    });	
};

/**
 * 展开或折叠查询商品品牌面板
 */
GoodsBrandHandler.queryGoodsBrand = function() {
	if (GoodsBrandHandler.isQueryBtnExpended) {
		GoodsBrandHandler.isQueryBtnExpended = false;
		mini.get("goods_brand_query").setText("展开查询");
		mini.get("goods_brand_query").setIconCls("icon-btn-expand");
	} else {
		GoodsBrandHandler.isQueryBtnExpended = true;
		mini.get("goods_brand_query").setText("折叠查询");
		mini.get("goods_brand_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryGoodsBrandDiv").toggle(500);
    setTimeout(function(){mini.get("goodsBrandPanel").doLayout();},600);
};

/**
 * 执行查询商品品牌的方法
 */
GoodsBrandHandler.doQueryGoodsBrand = function(){
	var bgbName = mini.get("query_bgbName").getValue();
	var bgbState = mini.get("query_bgbState").getValue();
	
    GoodsBrandHandler.goodsBrandGrid.load({bgbName: bgbName, bgbState: bgbState});
};

/**
 * 刷新页面数据及布局
 */
GoodsBrandHandler.afresh = function(){
	GoodsBrandHandler.doQueryGoodsBrand();
	mini.get("goodsBrandPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
GoodsBrandHandler.goodsBrandGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bgbDesc") {
     	if(e.value.length > 50){
     		e.cellHtml = e.value.substring(0,50) + "...";
     	}
     }
     
     if (column.field == "bgbState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsBrandHandler.updateGoodsBrandState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsBrandHandler.updateGoodsBrandState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

